package org.iteam.xlz.core.service.message;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.iteam.commons.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.messages.Messages;
import org.iteam.xlz.core.entity.user.UserInfo;

public class MessageService extends BaseService<Messages>{
	static MessageService instance;
	private MessageService(){}
	
	public static MessageService getInstance(){
		if(null==instance){
			instance=new MessageService();
		}
		return instance;
	}
	
	public long counts(){
		try {
			return super.counts("select count(1) from messages where 1=1");
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<Messages> query(long page,long rows){
		try {
			return super.query("select * from messages where 1=1 limit ?,?",(page-1)*rows,rows);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<Messages>();
		}
	}
	
	public boolean create(Messages model,String mobiles,String overduetime){
		Connection con=null;
		try {
			con=super.getConnection(false);
			SqlBuilder sql=SqlBuilder.createInsert("messages");
			String mid=UUID.randomUUID().toString().replace("-","").toUpperCase();
			model.setMid(mid);
			sql.addField("mid",model.getMid());
			sql.addField("content",model.getContent());
			sql.addField("title",model.getTitle());
			sql.addField("creator",model.getCreator());
			sql.addField("createtime",DateUtil.currentTime());
			
			String[] arrays=null;
			int total=0;
			if(StringUtils.isNotBlank(mobiles)){
				mobiles=mobiles.substring(0,mobiles.length()-1);
				arrays=mobiles.split(",");
				for(String s:arrays){
					UserInfo u=super.show(UserInfo.class,con,"select * from userinfo where mobile=?",s);
					if(null!=u){
						SqlBuilder sql1=SqlBuilder.createInsert("messagesuser");
						sql1.addField("muid",UUID.randomUUID().toString().replace("-","").toUpperCase());
						sql1.addField("objectid",model.getMid());
						sql1.addField("overduetime",overduetime);
						sql1.addField("uid",u.getUid());
						sql1.addField("msgtype",0);
						sql1.addField("createtime",DateUtil.currentTime());
						total+=super.updates(con, sql1.getSql(),sql1.getParams());
					}
				}
			}else{
				List<UserInfo> u=super.query(con,UserInfo.class,"select * from userinfo where status=0");
				arrays=new String[u.size()];
				int i=0;
				for(UserInfo user:u){
					arrays[i++]=user.getMobile();
					SqlBuilder sql1=SqlBuilder.createInsert("messagesuser");
					sql1.addField("muid",UUID.randomUUID().toString().replace("-","").toUpperCase());
					sql1.addField("objectid",model.getMid());
					sql1.addField("overduetime",overduetime);
					sql1.addField("uid",user.getUid());
					sql1.addField("msgtype",0);
					sql1.addField("createtime",DateUtil.currentTime());
					total+=super.updates(con, sql1.getSql(),sql1.getParams());
				}
			}
			
			int j=super.updates(con,sql.getSql(),sql.getParams());
			if(j==1&&total==arrays.length){
				super.commit(con);
				return true;
			}else{
				super.rollback(con);
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				super.rollback(con);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	}
	
	public boolean update(Messages model){
		SqlBuilder sql=SqlBuilder.createUpdate("messages");
		sql.addField("content",model.getContent());
		sql.addField("title",model.getTitle());
		sql.addWhere("mid",model.getMid());
		try {
			return super.updates(sql.getSql(),sql.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public Messages find(String mid){
		try {
			return super.show("select * from messages where 1=1 and mid=?",mid);
		} catch (SQLException e) {
			e.printStackTrace();
			return new Messages();
		}
	}
	
	public boolean delete(String mid){
		try {
			return super.updates("delete from messages where 1=1 and mid=?",mid)>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	
	
	public static void main(String[] args) throws SQLException, ClassNotFoundException {
		Class.forName("com.mysql.jdbc.Driver");
		Connection con=DriverManager.getConnection("jdbc:mysql://119.29.96.87:3306/cjcfh_xfx","root","y4yhl9t#tm");
		con.setAutoCommit(true);
		String sql=null;
		for(int i=0;i<100;i++){
/*			sql="insert into vote(vid,byvoter,voter,createtime) values('"+UUID.randomUUID().toString()+"','oQe_dt1zWg-bTQV1H8J8ZUCfgQ4Q',14,now())";
			con.prepareStatement(sql).execute();
			sql="insert into vote(vid,byvoter,voter,createtime) values('"+UUID.randomUUID().toString()+"','oQe_dt1MLm22Gn2iIo5E7ODHu-e8',23,now())";
			con.prepareStatement(sql).execute();
			sql="insert into vote(vid,byvoter,voter,createtime) values('"+UUID.randomUUID().toString()+"','oQe_dt1c3KtNS4q-rLIbPebw8CVs',38,now())";
			con.prepareStatement(sql).execute();
			sql="insert into vote(vid,byvoter,voter,createtime) values('"+UUID.randomUUID().toString()+"','oQe_dtzAmY-upqKKlZKgUQT4L6mI',15,now())";
			con.prepareStatement(sql).execute();*/
			
/*			sql="insert into vote(vid,byvoter,voter,createtime) values('"+UUID.randomUUID().toString()+"','oQe_dt8PJ0Ef0kL5Zqrmue7BUJ_g',26,now())";
			con.prepareStatement(sql).execute();
			sql="insert into vote(vid,byvoter,voter,createtime) values('"+UUID.randomUUID().toString()+"','oQe_dtwJRAr__5XUEbZNSOmZWy-U',41,now())";
			con.prepareStatement(sql).execute();
			sql="insert into vote(vid,byvoter,voter,createtime) values('"+UUID.randomUUID().toString()+"','oQe_dtzI1l3ROWGI-qmObLKzJMN0',46,now())";
			con.prepareStatement(sql).execute();
			sql="insert into vote(vid,byvoter,voter,createtime) values('"+UUID.randomUUID().toString()+"','oQe_dt1YpqztgDkA66NXlEVaWqcA',30,now())";
			con.prepareStatement(sql).execute();
			sql="insert into vote(vid,byvoter,voter,createtime) values('"+UUID.randomUUID().toString()+"','oQe_dt7TsIsXW7uTcdW1zeRxJeZw',25,now())";
			con.prepareStatement(sql).execute();
			sql="insert into vote(vid,byvoter,voter,createtime) values('"+UUID.randomUUID().toString()+"','oQe_dt4dweacNoJMqh3MmO7vj-k8',32,now())";
			con.prepareStatement(sql).execute();*/
			
			
			sql="insert into vote(vid,byvoter,voter,createtime) values('"+UUID.randomUUID().toString()+"','oQe_dt87PEgLyiwHdG2dJQT2E-gE',11,now())";
			con.prepareStatement(sql).execute();
			sql="insert into vote(vid,byvoter,voter,createtime) values('"+UUID.randomUUID().toString()+"','oQe_dtzROrtIlfcCirqzqlFZ6DbM',29,now())";
			con.prepareStatement(sql).execute();
			sql="insert into vote(vid,byvoter,voter,createtime) values('"+UUID.randomUUID().toString()+"','oQe_dt5VqKf2jzVJjYYwPFu2cG7A',53,now())";
			con.prepareStatement(sql).execute();
		}
		System.out.println("over");
	}
}
